//部分回款字段
var partialPayment = {};


$(function(){
    var url =  window.location.href.split('?')[1]
    var id = url.split('=')[1]
    // console.log(url,id)
    getData(id);
    orderInfoList()
    timeData();
    $('#arriveMethod').selectpicker();
    $('#edit_arriveMethod').selectpicker();
    loadCompanyList()
})

function timeData(){
    new Mdate("arriveTime", {
        //"dateShowBtn"为你点击触发Mdate的id，必填项
        acceptId: "arriveTime",
        //此项为你要显示选择后的日期的input，不填写默认为上一行的"dateShowBtn"
        beginYear: '1900',
        //此项为Mdate的初始年份，不填写默认为2000
        beginMonth: '9',
        //此项为Mdate的初始月份，不填写默认为1
        beginDay:'27',
        //此项为Mdate的初始日期，不填写默认为1
        format: "-"
        //此项为Mdate需要显示的格式，可填写"/"或"-"或".",不填写默认为年月日
    })
    new Mdate("edit_arriveTime", {
        //"dateShowBtn"为你点击触发Mdate的id，必填项
        acceptId: "edit_arriveTime",
        //此项为你要显示选择后的日期的input，不填写默认为上一行的"dateShowBtn"
        beginYear: '1900',
        //此项为Mdate的初始年份，不填写默认为2000
        beginMonth: '9',
        //此项为Mdate的初始月份，不填写默认为1
        beginDay:'27',
        //此项为Mdate的初始日期，不填写默认为1
        format: "-"
        //此项为Mdate需要显示的格式，可填写"/"或"-"或".",不填写默认为年月日
    })
}

function getData(id){
    $.ajax({
        url: '/order/finance/orderInfo',
        type: 'get',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded",
        data: {
            productOrderId:id,
        },
        success: function (data) {

        },
        error: function (err) {
            // $.alert('获取开发人员信息失败！');
            console.log(err);
        }
    })
}

function orderInfoList(){
    $.ajax({
        url: '/finishOrderProduct/selectOrderFinishProduct',
        type: 'POST',
        dataType: "json",
        data: {
            pageIndex: 1,
            pageSize:100,
            orderId:$('#orderCode').val()
        },
        success: (function (data) {
            if (data.code == "000000") {
                var html = '';
                $.each(data.data,function(index,item){
                    html = ` <div class="order_info buyProduct">
                            <div class="member">会员产品</div>
                            <div class="product_detail">
                                <div class="product_name">
                                    <span class="product_key">产品名称：</span>
                                    <span class="product_vaue">${item.productName}</span>
                                    <botton class="product_btn" onclick="payBack('2','${item.productId}','${item.productOrderNum}','${item.productName }','${item.productCategories}','${item.finishOrderProductNum}','${item.orderDetailType}','${item.finishOrderProductId}','${item.residueMoney}')">产品回款</botton>
                                </div>
                                <div class="product_box product_number">
                                    <span class="product_key">产品编号：</span>
                                    <span class="product_vaue">${item.finishOrderProductNum}</span>
                                </div>
                                <div class="" style="display: flex">
                                    <div class="product_payBack">
                                        <p class="payBack_value">${item.productFinalPrice}</p>
                                        <p class="payBack_key">产品成交价</p>
                                    </div>
                                    <div class="product_payBack">
                                        <p class="payBack_value">${item.paidMoney}</p>
                                        <p class="payBack_key">当前回款</p>
                                    </div>
                                    <div class="product_payBack">
                                        <p class="payBack_value">${item.residueMoney}</p>
                                        <p class="payBack_key">当前尾款</p>
                                    </div>
                                </div>
                            </div>
                        </div>`
                    $('#list').append(html)
                })


            }
        }),
        error: (function () {
            alert("失败");
        })
    });
}
/**
 * 加载回款公司信息
 */
function loadCompanyList() {
    var companyOptionHtml = '';
    var trueCompanyName = $("#companyNameId").val();
    if (trueCompanyName && trueCompanyName != "北京财税研究院" && trueCompanyName !="大成方略网络科技有限公司" && trueCompanyName !="北京大成方略财务顾问有限公司"
        && trueCompanyName != "北京大成方略纳税人俱乐部有限公司" && trueCompanyName !="大成方略纳税人俱乐部股份有限公司"){
        companyOptionHtml += '<option value="0">' + trueCompanyName + '</option>';
    }
    companyOptionHtml += '<option value="1">北京财税研究院</option>';
    companyOptionHtml += '<option value="2">大成方略网络科技有限公司</option>';
    companyOptionHtml += '<option value="3">北京大成方略财务顾问有限公司</option>';
    companyOptionHtml += '<option value="4">北京大成方略纳税人俱乐部有限公司</option>';
    companyOptionHtml += '<option value="5">大成方略纳税人俱乐部股份有限公司</option>';
    $('#arriveAccountType').html("");
    $('#arriveAccountType').append(companyOptionHtml);
    $('#arriveAccountType').selectpicker('refresh');
    $('#arriveAccountType1').html("");
    $('#arriveAccountType1').append(companyOptionHtml);
    $('#arriveAccountType1').selectpicker('refresh');
}

function payBack(type,id,productNumber,productName,productCategories,finishOrderProductNum,orderDetailType,finishOrderProductId,residueMoney){
    $('.modal').show()
    var wHeight = $(window).height();
    $('html').css({'height':wHeight,'overflow':'hidden'});
    $('body').css({'height':wHeight,'overflow':'hidden'});
    partialPayment.productId = id;
    partialPayment.productNumber = productNumber;
    partialPayment.productName = productName;
    partialPayment.productCategories = productCategories;
    partialPayment.finishOrderProductNum = finishOrderProductNum;
    partialPayment.orderDetailType = orderDetailType;
    partialPayment.finishOrderProductId = finishOrderProductId;
    partialPayment.residueMoney = residueMoney;
}

// //关闭弹窗
function closureBtn(){
    $('#edit_arriveTime').val('') //到款时间
    $('#edit_arriveMoney').val('') //到款金额
    $('#arriveAccountType1').val('').selectpicker('refresh'); //到款账号类型
    $('#edit_remark').val('') //备注
    $('#edit_arriveMoney').val('')
    $('#edit_paymentUnit').val($('#customerName').val());
    $('#edit_paymentAccount').val($('#paymentAccountVal').val())
    $('#edit_arriveMethod').val(0).selectpicker('refresh');
    $('#edit_paymentBank').val('');
    $('#edit_serial').val('')
    $('html,body').removeAttr('style');
    $('.modal').hide();
}

function resetBtn(){
    $('#arriveTime').val('') //到款时间
    // $('#arriveMoney').val('') //到款金额
    $('#arriveAccountType').val('').selectpicker('refresh'); //到款账号类型
    $('#remark').val('') //备注
    $('#paymentUnit').val($('#customerName').val());
    $('#paymentAccount').val($('#paymentAccountVal').val())
    $('#arriveMethod').val(0).selectpicker('refresh');
    $('#paymentBank').val('');
    $('#serial').val('')
}

function promptInfo(content) {
    $.alert({
        title: '温馨提示',
        content: content,
        buttons: {
            confirm: {
                text: '确认',
                btnClass: 'btn-primary'
            }
        }
    });
}

function inputMoney(element,num) {
    var returnMoneyId,arriveTime,arriveMoney,arriveUnitName,paymentUnit,paymentAccount,arriveMethod,serial,paymentBank,arriveAccountType,remark,orderId
    if(num == 0){
        arriveTime = $.trim($('#arriveTime').val());//回款日期
        returnMoneyId = $.trim($('#returnMoneyIdHide').val());//回款idarriveTime = $.trim($('#arriveTime').val());//回款日期
        arriveMoney = $.trim($('#arriveMoney').val());//到款金额
        arriveUnitName = $("#arriveAccountType").find("option:selected").text();//到款单位
        paymentUnit = $.trim($('#paymentUnit').val());//付款单位
        paymentAccount = $.trim($('#paymentAccount').val());//付款账号
        arriveMethod = $.trim($('#arriveMethod').val());//到款方式
        serial = $.trim($('#serial').val());//流水号
        paymentBank = $.trim($('#paymentBank').val());//付款银行
        arriveAccountType = $.trim($('#arriveAccountType').val());//到款账号类型
        remark = $.trim($('#remark').val());//备注orderId = $.trim($('#orderCode').val());//所属订单
        orderId = $.trim($('#orderCode').val());//所属订单
    }
    if(num == 1){
        arriveTime = $.trim($('#edit_arriveTime').val());//回款日期
        arriveMoney = $.trim($('#edit_arriveMoney').val());//到款金额
        arriveUnitName = $("#arriveAccountType1").find("option:selected").text();//到款单位
        paymentUnit = $.trim($('#edit_paymentUnit').val());//付款单位
        paymentAccount = $.trim($('#edit_paymentAccount').val());//付款账号
        arriveMethod = $.trim($('#edit_arriveMethod').val());//到款方式
        serial = $.trim($('#edit_serial').val());//流水号
        paymentBank = $.trim($('#edit_paymentBank').val());//付款银行
        arriveAccountType = $.trim($('#arriveAccountType1').val());//到款账号类型
        remark = $.trim($('#edit_remark').val());//备注
        orderId = $.trim($('#orderCode').val());//所属订单
    }

    if (!arriveTime) {
        promptInfo("到款日期不能为空!");
        return;
    }
    if ( 0.01 > arriveMoney){
        if (0.00 != $("#orderActualTotalAmount").text() && 0 != $("#orderActualTotalAmount").text()){
            promptInfo("到款金额不能为0!");
            return;
        }
    }
    if (!arriveMoney){
        promptInfo("到款金额不能为空!");
        return;
    }
    if (!paymentUnit) {
        promptInfo("付款单位不能为空!");
        return;
    }
    if (!arriveMethod) {
        promptInfo("到款方式不能为空!");
        return;
    }
    if (!arriveAccountType) {
        promptInfo("到款账号类型不能为空!");
        return;
    }
    if (!serial) {
        promptInfo("流水号不能为空!");
        return;
    }

    if(num == 0){
        if(Number($('#arriveMoney').val()) > Number($('#residueMoneyAmountspan').val())){
            promptInfo("到款金额不能大于尾款!");
            return;
        }
    }

    if(num == 1){
        if(Number($('#edit_arriveMoney').val()) > Number(partialPayment.residueMoney)){
            promptInfo("到款金额不能大于尾款!");
            return;
        }
    }

    $(element).prop('disabled', true);

    if(returnMoneyId == null || returnMoneyId == ""){
        var jsonData;
        if(num == 0){
            //新增回款
            jsonData = {
                "arriveType": 0,
                "arriveTime": arriveTime+' 00:00:00',
                "arriveMoney": arriveMoney,
                "arriveUnitName": arriveUnitName,
                "paymentUnit": paymentUnit,
                "paymentAccount": paymentAccount,
                "arriveMethod": arriveMethod,
                "arriveAccountType": arriveAccountType,
                "remark": remark,
                "serial": serial,
                "paymentBank": paymentBank,
                "orderId": orderId
            }

        }else{
            jsonData = {
                "arriveType": 1,
                "arriveTime": arriveTime+' 00:00:00',
                "arriveMoney": arriveMoney,
                "arriveUnitName": arriveUnitName,
                "paymentUnit": paymentUnit,
                "paymentAccount": paymentAccount,
                "arriveMethod": arriveMethod,
                "arriveAccountType": arriveAccountType,
                "remark": remark,
                "serial": serial,
                "paymentBank": paymentBank,
                "orderId": orderId,
                "productId":partialPayment.productId,
                "productNumber":partialPayment.productNumber,
                "productName":partialPayment.productName,
                "productCategories":partialPayment.productCategories,
                "finishOrderProductNum":partialPayment.finishOrderProductNum,
                "orderDetailType":partialPayment.orderDetailType,
                "finishOrderProductId":partialPayment.finishOrderProductId,
            }
        }
        $.ajax({
            url: '/returnMoney/addReturnMoneyJoinFlow',
            type: 'POST',
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(jsonData),
            success: (function (data) {
                if(data.code == '000000'){
                    if(num == 0){
                        $.alert({
                            title: '提示',
                            content: '保存成功！',
                            buttons: {
                                confirm: {
                                    text: '确认',
                                    btnClass: 'btn-primary',
                                    action: function () {
                                        window.location.reload();
                                    }
                                }
                            }
                        });
                    }
                   if(num == 1){
                       $.alert({
                           title: '提示',
                           content: '保存成功！',
                           buttons: {
                               confirm: {
                                   text: '确认',
                                   btnClass: 'btn-primary',
                                   action: function () {
                                       closureBtn();
                                       window.location.reload();

                                   }
                               }
                           }
                       });

                   }
                }else {
                    $.alert(data.message);
                    $(element).prop('disabled', false);
                }
            }),
            error: (function (res) {
                $.alert("保存失败");
                $(element).prop('disabled', false);
            })
        })
    }
}

